package com.jh.user.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jh.user.business.UserBalanceBusiness;
import com.jh.user.business.UserCoinBusiness;
import com.jh.user.pojo.RankingList;
import com.jh.user.pojo.UserAccount;

import cn.jh.common.utils.CommonConstants;
import cn.jh.common.utils.TokenUtil;



@Controller
@EnableAutoConfiguration
public class UserCoinService {

	private static final Logger LOG = LoggerFactory.getLogger(UserCoinService.class);
	
	
	@Autowired
	private UserCoinBusiness  userCoinBusiness;
	
	
	@Autowired 
	private UserBalanceBusiness userBalBusiness;
	
	/**获取用户的积分流水**/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/coin/query/{token}")
	public @ResponseBody Object pageCoinQuery(HttpServletRequest request,  
			 @PathVariable("token") String token,
			 @RequestParam(value = "page", defaultValue = "0", required = false) int page,
			 @RequestParam(value = "size", defaultValue = "20", required = false) int size,
			 @RequestParam(value = "order", defaultValue = "DESC", required = false) Sort.Direction direction,
			 @RequestParam(value = "sort", defaultValue = "createTime", required = false) String sortProperty			
			){
		
		Map map = new HashMap();
		long userId;
		try{
			userId = TokenUtil.getUserId(token);
		}catch (Exception e) {
			map.put(CommonConstants.RESP_CODE,CommonConstants.ERROR_TOKEN);
			map.put(CommonConstants.RESP_MESSAGE, "token无效");
			return map;		
		}
		
		Pageable pageable = new PageRequest(page, size, new Sort(direction,sortProperty));

		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		map.put(CommonConstants.RESULT, userCoinBusiness.queryUserCoinHistoryByUserid(userId, pageable));
		return map;
	
	}
	
	
	/**更新用户的积分*/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/coin/update/userid")
	public @ResponseBody Object updateUserCoinByUserid(HttpServletRequest request,   
			@RequestParam(value = "user_id") long userId,
			@RequestParam(value = "coin",  defaultValue = "0",  required=false) int coin,
			@RequestParam(value = "order_code") String orderCode,
			@RequestParam(value = "addorsub",  defaultValue = "0",  required=false) String addorsub
			){		
		Map map = new HashMap();
		UserAccount userAccount = userBalBusiness.queryUserAccountByUserid(userId);
		
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		map.put(CommonConstants.RESULT, userCoinBusiness.updateUserCoin(userAccount, coin, addorsub, orderCode));
		return map;
		
	}
	
	
	
	
	/**更新用户的账户信息 */
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/coin/update/{token}")
	public @ResponseBody Object updateUserCoin(HttpServletRequest request,   
			@PathVariable("token") String token, 
			@RequestParam(value = "coin",  defaultValue = "0",  required=false) int coin,
			@RequestParam(value = "order_code") String  ordercode,
			@RequestParam(value = "addorsub",  defaultValue = "0",  required=false) String addorsub
			){		
		
		Map map = new HashMap();
		long userId;
		try {
			userId = TokenUtil.getUserId(token);
		} catch (Exception e) {
			map.put(CommonConstants.RESP_CODE,CommonConstants.ERROR_TOKEN);
			map.put(CommonConstants.RESP_MESSAGE, "token无效");
			return map;		
		}	
 
		UserAccount userAccount = userBalBusiness.queryUserAccountByUserid(userId);
		
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		map.put(CommonConstants.RESULT, userCoinBusiness.updateUserCoin(userAccount, coin, addorsub, ordercode));
		return map;
		
	}
	
	/**积分排行 */
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/coin/ranking/query")
	public @ResponseBody Object queryUserCoin(HttpServletRequest request,   
			@RequestParam(value = "brand_id") int brandid
			){		
		
		Map map = new HashMap();
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		map.put(CommonConstants.RESULT, userCoinBusiness.queryRankingListCoin(brandid));
		return map;
	}
	
	/**收益排行 */
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/earnings/ranking/query")
	public @ResponseBody Object queryUserEarnings(HttpServletRequest request,   
			@RequestParam(value = "brand_id") int brandid
			){		
		Map map = new HashMap();
		List<RankingList> rankingList = userCoinBusiness.queryRankingListEarnings(brandid);
		for(int i=3;i<rankingList.size();i++) {
			Date date = rankingList.get(i).getUpdatetime();
			long id = rankingList.get(i).getId();
			int coin = rankingList.get(i).getCion().intValue();
			int day1 = date.getDay();
			Date dat = new Date();
			int day2 = dat.getDay();
			/*每天同一个brandid只修改一次金额*/
			if(day1!=day2) {
				Random ran = new Random();
				int num = ran.nextInt(10000);
				rankingList.get(i).setCion(new BigDecimal(num+coin));
				rankingList.get(i).setId(id);
				rankingList.get(i).setUpdatetime(dat);
				userCoinBusiness.addRankingList(rankingList.get(i));
			}
			
		}
		List<RankingList> rankingList2 = userCoinBusiness.queryRankingListEarnings(brandid);
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		map.put(CommonConstants.RESULT, rankingList2);
		return map;
	}
	
	/****添加排名信息***/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/coin/ranking/add")
	public @ResponseBody Object addUserCoin(HttpServletRequest request, 
			/**修改ID***/
			@RequestParam(value = "id",defaultValue = "0",required=false) long id,
			
			@RequestParam(value = "brand_id") long brandid,
			/*** 用户姓名* ***/
			@RequestParam(value = "username") String username,
			/**积分**/
			@RequestParam(value = "cion")  BigDecimal cion,
			/**收益**/
			@RequestParam(value = "earnings") BigDecimal earnings
			
			){		
		
		Map map = new HashMap();
		
		RankingList rankingList  = userCoinBusiness.queryRankingListByid(id);
		if(rankingList==null){
			rankingList=new RankingList();
			rankingList.setCreateTime(new Date());
		}
		rankingList.setBrandId(brandid);
		rankingList.setCion(cion);
		rankingList.setUsername(username);
		rankingList.setEarnings(earnings);
		rankingList.setUpdatetime(new Date());
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		map.put(CommonConstants.RESULT, userCoinBusiness.addRankingList(rankingList));
		return map;
	}
	
	

}
